package com.wechat.wins.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public final class SocketUtils {

    public static void close(final Socket s) {
        try {
            if (s != null) {
                s.shutdownInput();
                s.shutdownOutput();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    public static byte[] readBytes(final DataInputStream in, final int length) throws IOException {
        int r = 0;
        final byte[] data = new byte[length];
        if (in != null) {
            while (r < length) {
                r += in.read(data, r, length - r);
            }
        }

        return data;
    }

    public static void writeBytes(final DataOutputStream out, final byte[] bytes, final int length) throws IOException {
        if (out != null) {
            out.writeInt(length);
            out.write(bytes, 0, length);
            out.flush();
        }
    }
}